package controllers;

import java.util.ArrayList;
import java.util.List;

import models.ActionItem;
import models.Idea;
import models.Organization;
import models.Tag;
import models.Topic;
import models.User;
import play.mvc.Controller;

public class TagsController extends Controller {
	/**
	 * 
	 * @author Yomna ElRashidi 
	 * @version 1.0
	 * 
	 * */

	/**
	 * @author Yomna El Rashidi
	 * 
	 *         finds whether a user is allowed to create a tag and check if he
	 *         has the authority to do so within this organisation or not
	 * 
	 * @param orgID
	 * 
	 *            the ID of the organization in the session , where the user
	 *            wants to create the tag in
	 * 
	 * */

	public static void isAllowedTagCreation(long orgID) {
		String userName = session.get("username");
		User user = null;
		if (userName != null) {
			user = User.find("byUsername", userName).first();
		}
		if (user == null) {
			return;
		}
		Organization org = Organization.find("byID", orgID).first();
		boolean allowed = false;
		if (org != null) {
			allowed = org.allowingTagCreationForDevelopers;

		}
		render(allowed, orgID);
	}

	/**
	 * @author Yomna El Rashidi
	 * 
	 *         creates a tag if it is not already in the organization and also
	 *         subscribe the user in it , or if it is already available the user
	 *         gets subscribed to it only.
	 * 
	 * @param name
	 *            The name of the tag to be created .
	 * @orgID 
	 * 			  The ID of the organization to return to 
	 * 
	 */
	public static void FindorCreateTag(long orgID, String name) {

		String userName = session.get("username");
		User user = null;
		if (userName != null) {
			user = User.find("byUserName", userName).first();
		}
		if (user == null) {
			return;
		}
		Tag tag = Tag.find("byName", name).first();

		if (tag == null) {
			tag = new Tag(name).save();
		}

		user.subscribedTags.add(tag);
		Browse.viewOrganization(orgID);

	}

	/**
	 * Find the topics, ideas, and actionItems tagged with this tag.
	 * 
	 * @param tagId
	 *            The tag id
	 * @author Ahmed Mahmoud
	 */
	public static void getTaggedWith(long tagId) {
		String username = session.get("username");
		notFoundIfNull(username);
		User user = User.find("username", username).first();
		notFoundIfNull(user);
		Tag tag = Tag.findById(tagId);
		notFoundIfNull(tag);
		List<Idea> ideasTaggedWith = tag.relatedIdeas;
		List<Idea> allowedIdeasTaggedWith = new ArrayList<Idea>();
		for (Idea idea : ideasTaggedWith) {
			if (idea.isAllowedToBeViewedToIdeaDevelopers()) {
				if (idea.topic.entity.organization.isSecret()) {
					if (idea.topic.entity.organization.isMember(user.id)) {
						allowedIdeasTaggedWith.add(idea);
					}
				} else {
					allowedIdeasTaggedWith.add(idea);
				}
			}
		}
		List<Topic> topicsTaggedWith = tag.relatedTopics;
		List<Topic> allowedTopicsTaggedWith = new ArrayList<Topic>();
		for (Topic topic : topicsTaggedWith) {
			if (topic.isAllowedToBeViewedToIdeaDevelopers()) {
				if (topic.entity.organization.isSecret()) {
					if (topic.entity.organization.isMember(user.id)) {
						allowedTopicsTaggedWith.add(topic);
					}
				} else {
					allowedTopicsTaggedWith.add(topic);
				}
			}
		}
		
		render(allowedIdeasTaggedWith, allowedTopicsTaggedWith);
	}

}
